<template>
  <div class="form-box">
    <div class="form-header">基本信息</div>
    <div class="form-body">
      <el-form :model="model" :rules="rules" ref="model" label-width="100px" class="demo-ruleForm">
        <el-form-item label="名称：" prop="bannerName">
          <el-input v-model="model.bannerName"></el-input>
        </el-form-item>
        <el-form-item label="图片：" prop="img">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="model.img" :src="model.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="链接：" prop="linkType1" class="radio-group">
          <el-radio-group v-model="model.linkType" style="float: left">
            <el-radio label="1">
              关联课程
              <!--          <template>-->
              <el-button type="primary" @click="isShowChooseCourse = true">添加课程</el-button>
              <!--          </template>-->
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="linkType2" class="radio-group">
          <el-radio-group v-model="model.linkType" style="float: left">
            <el-radio label="2">
              外部链接
              <!--          <template>-->
              <el-input style="display: inline-block;width: 200px" v-model="model.linkUrl"
                        placeholder="请输入Banner名称"></el-input>
              <!--          </template>-->
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('model')">{{$t('action.comfirm')}}</el-button>
          <el-button @click="close">{{$t('action.cancel')}}</el-button>
        </el-form-item>
        <ChooseCourse v-if="isShowChooseCourse" :checkbox="false" @closePop="closeCourseChoose"/>
      </el-form>
    </div>
  </div>
</template>

<script>
  import ChooseCourse from "../../../common/ChooseCourse";

  export default {
    name: "AddBanner",
    components: {ChooseCourse},
    data() {
      var checkLinkUrl1 = (rule, value, callback) => {
        if (!this.model.linkType) return callback(new Error('请选择链接类型'));
        if (parseInt(this.model.linkType) == 1 && !this.model.courseId) return callback(new Error('请选择课程'));
      };
      var checkLinkUrl2 = (rule, value, callback) => {
        if (!this.model.linkType) return callback(new Error('请选择链接类型'));
        if (parseInt(this.model.linkType) == 2 && !this.model.linkUrl) return callback(new Error('请填写外部链接'));
      };
      return {
        model: {
          bannerName: "",
          img: "",
          linkUrl: "",
          linkType: "1",
          courseId: "",
          courseName: "",
          status: "",
        },
        rules: {
          bannerName: [
            {required: true, message: '请输入Banner名称', trigger: 'blur'},
          ],
          img: [
            {required: true, message: '请选择Banner图片', trigger: 'blur'},
          ],
          linkType1: [
            {required: true, trigger: 'blur', type: Number, validator: checkLinkUrl1},
          ],
          linkType2: [
            {required: true, trigger: 'blur', type: Number, validator: checkLinkUrl2},
          ],
        },
        isShowChooseCourse: false,
      }
    },
    created() {
      if (this.$route.query.id || this.id) this.getBanner(this.$route.query.id || this.id);
    },
    methods: {
      getBanner(id) {
        this.$api.mall.getBanner({id: id}).then(
          res => {
            this.model = res.data;
            this.model.linkType = (res.data.linkUrl) ? 1 : 2;
          }
        )
      },
      close(data) {
        this.$emit("closePop", data)
      },
      closeCourseChoose(data) {
        this.isShowChooseCourse = false;
        console.log(data)
      },
      onSubmit(formName) {
        console.log(this.$refs[formName])
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    }
  }
</script>

<style lang="scss" scoped>
  .form-box {
    margin: 10px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, .05);

    .form-header {
      height: 42px;
      line-height: 42px;
      padding: 0 15px;
      border-bottom: 1px solid #f6f6f6;
      color: #333;
      border-radius: 2px 2px 0 0;
      font-size: 14px;
      text-align: left;
      font-weight: bolder;
    }

    .form-body {
      position: relative;
      padding: 10px 15px;
      line-height: 24px;

      .avatar {
        width: 200px;
      }
    }
  }
  .el-form {
    width: 40%;
    margin: 0 auto
  }

  .radio-group.is-error {
    color: red;

    .el-form-item__error {
      left: 86px;
    }
  }
</style>
